var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    choose: [0, 0],
    modelIndex: 0,
    specIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('回收操作界面');
    util.postJSON('product/selectAllModelSpes', {}, returnData => {
      var modelsList = returnData.modelSpes;
      var specList = modelsList[0].specifications;

      this.setData({ modelSpecArray: [modelsList, specList] })
    })
  },
  // //二级联动时用的方法
  // bindMultiPickerChange: function (e) {
  //   console.log('picker发送选择改变，携带值为', e.detail.value)
  //   this.setData({
  //     multiIndex: e.detail.value
  //   })
  // },
  // bindMultiPickerColumnChange: function (e) {
  //   console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
  //   var data = {
  //     multiArray: this.data.multiArray,
  //     multiIndex: this.data.multiIndex
  //   };

  //   data.multiIndex[e.detail.column] = e.detail.value;
  //   console.log("选择的是" + data.multiArray[0][e.detail.column])

  //   for (var i = 0; i < this.data.specList.length; i++) {
  //     // console.log(data.multiArray[0][0])
  //     // console.log(data.multiArray[0][0] == res.data.data.specifications[i].name)
  //     if (data.multiArray[0][data.multiIndex[0]] == this.data.specList[i].name) {
  //       data.multiArray[1] = this.data.specList[i].types
  //       break
  //     }
  //   }
  //   this.setData(data);
  // },
  saveChoose (e) {
    var choose = e.detail.value;
    var modelSpecArray = this.data.modelSpecArray;
    var modelsList = modelSpecArray[0];
    var specList = modelsList[choose[0]].specifications;

    this.setData({
      modelSpec: modelsList[choose[0]].name + " " + specList[choose[1]].name
    })
  },
  columnChange (e) {
    var key = e.detail.value;
    var column = e.detail.column;
    var modelSpecArray = this.data.modelSpecArray;
    var modelIndex = this.data.modelIndex;
    var specIndex = this.data.specIndex;

    if (column == 0) {
      modelIndex = key;
      specIndex = 0;
    } else {
      specIndex = key
    }
    var modelsList = modelSpecArray[0];
    var specList = modelsList[modelIndex].specifications;
    this.setData({
      specIndex: specIndex,
      modelIndex: modelIndex,
      choose: [modelIndex, specIndex],
      modelSpecArray: [modelsList, specList],
      modelSpec: modelsList[modelIndex].name + " " + specList[specIndex].name
    })
  },
  scan () {
    var that = this;
    wx.scanCode({
      onlyFromCamera: false,
      scanType: ['barCode'],
      success: function (res) {
        that.setData({ bar: res.result })
      }
    })
  },

  formSubmit (e) {
    var sn = e.detail.value.sn;
    var choose = this.data.choose;
    var description = e.detail.value.bz;
    var modelSpecArray = this.data.modelSpecArray;
    var modelSpec = e.detail.value.modelSpec;
    var modelId = modelSpecArray[0][choose[0]].id;
    var specification_id = modelSpecArray[1][choose[1]].id;

    if (sn == '') { util.showTips('SN不能为空'); return }
    if (modelSpec == '') { util.showTips('设备型号不能为空'); return }

    this.setData({ bar: '' })
    util.postJSON('product/insertOneRecycle', {
      modelId: modelId, sn: sn, specification_id: specification_id, description: description
    }, returnData => {
      util.showTips('设备' + sn + '回收成功');
    })
  },
})